package com.bw.servlet;

import com.bw.dao.UserDao;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/***************************
 FileAction:

 CreateTime:2022-04-07 10:19
 Author:Aaking-aline
 ****************************/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @SneakyThrows
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        request.getParameter("username");
        String password = request.getParameter("password");


        UserDao userDao = new UserDao();
        boolean b = userDao.selectUserByUsername(username);
        if(b){
            // 这里证明 该用户名已存在
            response.sendRedirect("error.jsp");
            return ;
        }

        userDao.insertUser(username,password);
        // 注册成功以后  直接让它跳转到  登录页了
        // 其实也可以这么做  就是成功后让它跳转到  一个信息提示页提示注册成功  然后页面内有个定时器  3秒钟后再跳回登录页
        response.sendRedirect("index.jsp");
    }
}
